package com.midou.administration.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.midou.administration.bo.DelDesk;
import com.midou.administration.bo.DeskBo;
import com.midou.administration.bo.TypeDesk;
import com.midou.administration.entity.Desk;
import com.midou.administration.entity.DeskType;
import com.midou.administration.mapper.DeskMapper;
import com.midou.administration.mapper.DeskTypeMapper;
import com.midou.administration.service.DeskService;
import com.midou.administration.utils.QRCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.RequestBody;
import sun.security.krb5.internal.crypto.Des;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author MrChen
 * @since 2021-03-24
 */
@Service
public class DeskServiceImpl implements DeskService {
    @Resource
    private DeskMapper deskMapper;
    @Autowired
    private DeskTypeMapper deskTypeMapper;
    		//保存地址
    private String staticPath = System.getProperty("user.dir")+"\\midou-restaurant-administration\\src\\main\\resources\\static";
    public List<Desk> queryDesk(TypeDesk typeDesk){
        return deskMapper.queryDesk(typeDesk);
    }
    public Integer save(Desk desk){
        QueryWrapper qw = new QueryWrapper();
        int count = deskMapper.insert(desk);

        String text = "http://localhost:12587/"+desk.getDeskId();  //这里设置自定义网站url或文字
        String logoPath = staticPath+"\\logo\\logo.png"; //二维码图片
        String destPath = staticPath+"\\qr";
        try {
            System.out.println();
            //将图片路径存入数据库
            desk.setDeskQrCode(QRCodeUtils.encode(text, logoPath, destPath, true));
        } catch (Exception e) {
            e.printStackTrace();
        }
        qw.eq("desk_id",desk.getDeskId());
        int num = deskMapper.update(desk,qw);
        if (count>0 && num>0){
            return 1;
        }
        return 0;
    }
    public String queryQrPath(Integer id){
        String path = deskMapper.queryQrPath(id);
        //转换为Base64编码返回
        return QRCodeUtils.ImageToBase64ByLocal(staticPath+"\\qr\\"+path);
    }
    public Integer delDesk(DelDesk delDesk){
        Integer count = deskMapper.delDesk(delDesk.getDid());
        if (count>0){
            File file = new File(staticPath+"\\qr\\"+delDesk.getQr());
            if (file.exists()){
                file.delete();
            }
            return 1;
        }
        return 0;
    }

    public Integer modifyDesk(Desk desk){
        File file = new File(staticPath+"/"+desk.getDeskQrCode());
        if (file.exists()){
            file.delete();
        }
        String text = "http://localhost:12587/"+desk.getDeskId();  //这里设置自定义网站url或文字
        String logoPath = staticPath+"/logo/logo.png"; //二维码图片
        String destPath = staticPath+"\\qr";
        try {
            //将图片路径存入数据库
            desk.setDeskQrCode(QRCodeUtils.encode(text, logoPath, destPath, true));
        } catch (Exception e) {
            e.printStackTrace();
        }
        QueryWrapper qw = new QueryWrapper();
        qw.eq("desk_id",desk.getDeskId());

        return deskMapper.update(desk,qw);
    }


    public Desk queryDeskOne(Integer id){
        QueryWrapper qw = new QueryWrapper();
        qw.eq("desk_id",id);
        return deskMapper.selectOne(qw);
    }
    /**
     * 小程序查找桌台信息
     */
    public List<Desk> queryXcx(Integer id){
        return deskMapper.queryXcx(id);
    }
    /**
     * 根据类型查找桌台信息
     */
    public List<Desk> queryName(Integer id,Integer did){
        return deskMapper.queryName(id,did);
    }

    /**
     * 按照商户id查询该商户下所有就餐人数
     * @param buid
     * @return
     */
    public int doBuidFindSumPeople(int buid){
        int people = deskMapper.doBuidFindSumPeople(buid);
        return people;
    }

    @Override
    public List<Desk> queryDeskByTypeId(int id,int buId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("desk_type_id",id);
        queryWrapper.eq("bu_id",buId);
        queryWrapper.eq("desk_status",0);
        List<Desk> list = this.deskMapper.selectList(queryWrapper);
        return list;
    }

    //二级菜单区域信息
    public List<DeskType> quyuQuery(DeskBo deskBo){
        QueryWrapper queryWrapper1=new QueryWrapper();
        queryWrapper1.eq("bu_id",deskBo.getBuid());
        List<DeskType> deskTypeList=deskTypeMapper.selectList(queryWrapper1);
        return deskTypeList;
    }
    //二级菜单区域桌台信息
    public List<Desk> zhuotaiQuery(DeskBo deskBo){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("desk_type_id",deskBo.getDeskId());
        queryWrapper.eq("bu_id",deskBo.getBuid());
        List<Desk> deskList=deskMapper.selectList(queryWrapper);
        return  deskList;

    }
}
